package longrise.phone.com.bjjt_henan.bindinginformation;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.ILFMsgListener;
import com.longrise.android.LFView;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.util.UiUtil;

import longrise.phone.com.bjjt_henan.Adapter.SuggestAdapter;
import longrise.phone.com.bjjt_henan.MainActivity;
import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.ApiImpI;
import longrise.phone.com.bjjt_henan.Utils.DialogUtils;

/**
 * Created by Administrator on 2016/8/16.
 */
public class FeedBackView extends LFView implements View.OnClickListener, ILFMsgListener {

    private View view;

    private Context context;

    private BaseApplication application;

    private ListView sugLView;

    private SuggestAdapter sugAdapter;

    private EntityBean[] sugBeans;

    private View emptyIV;//无数据时的背景色

    private TextView commitTV;

    private EditText contentET;

    private boolean isFinish;

    FeedBackView(Context context) {
        super(context);
        this.context = context;
    }

    public FeedBackView(Context context, boolean isFinish) {
        super(context);
        this.context = context;
        this.isFinish = isFinish;
    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter parameter = new FormParameter();
        parameter.setShowtitle(false);
        return parameter;
    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        if (null == context) {
            return;
        }
        view = LayoutInflater.from(context).inflate(R.layout.view_feedback, null);
        if (null == view) {
            return;
        }
        //标题栏
        TextView titleTV = (TextView) view.findViewById(R.id.tv_title);
        titleTV.setText("用户反馈");
        view.findViewById(R.id.hn_btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isFinish) {
                    ((MainActivity) context).backToMainView();
                } else {
                    OnDestroy();
                    closeForm();
                }
            }
        });
        //提交反馈操作
        commitTV = (TextView) view.findViewById(R.id.tv_register);
        commitTV.setVisibility(View.VISIBLE);
//        Drawable drawable = context.getResources().getDrawable(R.mipmap.add);
//        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        commitTV.setCompoundDrawables(null, null, null, null);
        commitTV.setText("提交");
        contentET = (EditText) view.findViewById(R.id.feedback_et_content);

        emptyIV = view.findViewById(R.id.empty_tv);
        sugLView = (ListView) view.findViewById(R.id.feedback_lv);
        sugAdapter = new SuggestAdapter(context);
        sugLView.setAdapter(sugAdapter);

        setListen(true);
        application = ((MainActivity) context).getAppData();
        addILFMsgListener(this);
    }

    private void setListen(Boolean flag) {
        if (commitTV != null) {
            commitTV.setOnClickListener(flag ? this : null);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_register:
                showDialog();
                break;
            default:
                break;
        }
    }

    private void showDialog() {
        String content = contentET.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            UiUtil.showToast(context, "请输入您的反馈意见或建议！");
            return;
        }
        commit(content);
    }

    //提交反馈内容
    private void commit(String content) {
        String userflag = application.getKckpName();
        String token = application.getToken();
        EntityBean baseBean = application.getLoginInfo().getBean("userinfo");
        String phone = baseBean.getString("mobilephone");
        String name = baseBean.getString("name");
        if (TextUtils.isEmpty(name)) {
            name = "未知";
        }
        DialogUtils.showDialog(context);
        ApiImpI.getInstance(context).feedback(userflag, token, name, phone, content, new ApiImpI.RequestCallBack() {
            @Override
            public void onError(String error) {
                DialogUtils.closeDialog();
                if (!TextUtils.isEmpty(error)) {
                    UiUtil.showToast(context, error);
                }
            }

            @Override
            public void onSuccess(Object result) {
                DialogUtils.closeDialog();
                EntityBean bean = (EntityBean) result;
                String restate = bean.getString("restate");
                String redes = bean.getString("redes");
                if (TextUtils.equals(restate, "1")) {
                    refresh();
                    contentET.setText("");
                }
                UiUtil.showToast(context, redes);
            }
        });
    }

    @Override
    public Object onLFMsg(int i, Object... objects) {
        if (i == -10) {//点击返回按键
            if (isFinish) {
                ((MainActivity) context).backToMainView();
            } else {
                OnDestroy();
                closeForm();
            }
        }
        return null;
    }

    @Override
    public void refresh() {
        String uid = application.getKckpName();
        String token = application.getToken();
        DialogUtils.showDialog(context);
        ApiImpI.getInstance(context).getAllSuggests(uid, token, new ApiImpI.RequestCallBack() {
            @Override
            public void onError(String error) {
                DialogUtils.closeDialog();
                if (!TextUtils.isEmpty(error)) {
                    UiUtil.showToast(context, error);
                }
            }

            @Override
            public void onSuccess(Object result) {
                DialogUtils.closeDialog();
                if (view == null) {
                    return;
                }
                EntityBean bean = (EntityBean) result;
                String restate = bean.getString("restate");
                if (TextUtils.equals(restate, "1")) {
                    sugBeans = bean.getBeans("data");
                    if (null == sugBeans || sugBeans.length <= 0) {
                        sugLView.setEmptyView(emptyIV);
                    } else {
                        emptyIV.setVisibility(View.GONE);
                    }
                    if (null != sugAdapter) {
                        sugAdapter.setDataLists(sugBeans);
                        sugAdapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        setListen(false);
        view = null;
        if (null != sugLView) {
            sugLView.setAdapter(null);
            sugLView = null;
        }
        removeILFMsgListener(this);
        sugBeans = null;
    }
}
